package tvv.levelup;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import tvv.levelup.controller.NewsController;
import tvv.levelup.models.Comment;
import tvv.levelup.models.News;

import java.util.Iterator;
import java.util.List;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext ac = new FileSystemXmlApplicationContext("/config/ApplicationContext.xml");
        NewsController newsController = ac.getBean(NewsController.class);

        News news1 = newsController.saveNews("news1");
        News news2 = newsController.saveNews("news2");

        Comment comment1 = newsController.saveComment("message1", 24L);
        Comment comment2 = newsController.saveComment("message2", 123L);
        Comment comment3 = newsController.saveComment("message3", 54L);

        List<News> newses = newsController.getAllNews();
        showAllNews(newses);

        newsController.addCommentToNews(news1.getId(), comment1.getId());
        newsController.addCommentToNews(news1.getId(), comment2.getId());
        newsController.addCommentToNews(news2.getId(), comment3.getId());
        newses = newsController.getAllNews();
        showAllNews(newses);

    }

    public static void showAllNews(List<News> newses) {
        Iterator<News> iterator = newses.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
